package org.dromara.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.business.domain.LzshBusinessInfoDataTotal;
import org.dromara.business.domain.bo.LzshTemplateBo;
import org.dromara.business.domain.vo.LzshTemplateVo;
import org.dromara.business.service.ILzshTemplateService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 表单管理
 *
 * @author Lion Li
 * @date 2024-08-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/template")
public class LzshTemplateController extends BaseController {

    private final ILzshTemplateService lzshTemplateService;

    /**
     * 查询表单管理列表
     */
    @SaCheckPermission("business:template:list")
    @GetMapping("/list")
    public List<LzshTemplateVo> list(LzshTemplateBo bo, PageQuery pageQuery) {
        return lzshTemplateService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询表单总数
     */
    @SaCheckPermission("business:template:count")
    @GetMapping("/count")
    public long count(LzshTemplateBo bo) {
        return lzshTemplateService.queryCount(bo);
    }

    /**
     * 导出表单管理列表
     */
    @SaCheckPermission("business:template:export")
    @Log(title = "表单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(LzshTemplateBo bo, HttpServletResponse response) {
        List<LzshTemplateVo> list = lzshTemplateService.queryList(bo);
        ExcelUtil.exportExcel(list, "表单管理", LzshTemplateVo.class, response);
    }

    /**
     * 获取表单管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:template:query")
    @GetMapping("/{id}")
    public R<LzshTemplateVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(lzshTemplateService.queryById(id));
    }

    /**
     * 新增表单管理
     */
    @SaCheckPermission("business:template:add")
    @Log(title = "表单管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody LzshTemplateBo bo) {
        Long userId = LoginHelper.getUserId();
        return toAjax(lzshTemplateService.insertByBo(bo));
    }

    /**
     * 修改表单管理
     */
    @SaCheckPermission("business:template:edit")
    @Log(title = "表单管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody LzshTemplateBo bo) {
        return toAjax(lzshTemplateService.updateByBo(bo));
    }

    /**
     * 删除表单管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:template:remove")
    @Log(title = "表单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(lzshTemplateService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 按模板统计数量
     */
    @GetMapping("/api/templateNum")
    public R<Map<String,List<LzshBusinessInfoDataTotal.totalData2>>>getTemplateNum(){
        return R.ok(lzshTemplateService.getTemplateNum());
    }
}
